//
//  WmcPhotoPreviewVC+AspectRatio.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/9.
//

import UIKit
import YPGraphicEngine

extension WmcPhotoPreviewVC {
    /// 设置画幅比例
    func setAspectRatio(_ ratio: YPGESunglasses.AspectRatio,
                        animated: Bool = false) {
        // 计算后的画幅高度, 承接优化计算性能
        let aspectHeight = ratio.aspectHeight
        let ratioLevel = ratio.ratioLevel
        // 布局动画
        if animated {
            // 准备开始布局
            self.view.setNeedsUpdateConstraints()
            UIView.animate(withDuration: 0.3,
                           delay: 0,
                           options: .curveEaseInOut) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)
                // 强制布局
                weakSelf.view.layoutIfNeeded()
            }
        } else {
            setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)
//            view.layoutIfNeeded()
        }
    }

    func setLayoutStyle(aspectHeight: CGFloat, ratioLevel: YPGESunglasses.AspectRatio.RatioLevel) {
        // 2、设置布局
        setViewLayout(ratioLevel, aspectHeight: aspectHeight)
        // 3、设置样式
        setViewStyle(ratioLevel)
    }

    /// 设置布局
    func setViewLayout(_ ratioLevel: YPGESunglasses.AspectRatio.RatioLevel, aspectHeight: CGFloat) {
        // 底部模式选择视图不变
        let contentHeight = Self.NavHeight + Wmc_safeAreaBottom + YPGESunglasses.AspectRatio.ar_16_9.aspectHeight
        var bottomViewHeight = Wmc_screenHeight - contentHeight
        if bottomViewHeight < 0 {
            bottomViewHeight = 42.wmc_autoVer
        }
        bottomView.snp.remakeConstraints { make in
            make.bottom.equalTo(-Wmc_safeAreaBottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(bottomViewHeight)
        }
        switch ratioLevel {
        case .low: // 都不透明
            // 预览图布局
            imagePreview.snp.remakeConstraints { make in
                make.top.equalTo(navView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(bottomView.snp.top)
                make.top.equalTo(imagePreview.snp.bottom)
            }
        case .lowHeight: // 顶部透明，底部不透明
            // 预览图布局
            imagePreview.snp.remakeConstraints { make in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(bottomView.snp.top)
                make.top.equalTo(imagePreview.snp.bottom)
            }
        case .medium: // 顶部不透明，底部透明
            // 预览图布局
            imagePreview.snp.remakeConstraints { make in
                make.top.equalTo(navView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(bottomView.snp.top)
                make.height.equalTo(168.wmc_autoVer)
            }
        case .mediumHeight,
             .height: // 顶部底部全透明
            // 预览图布局
            imagePreview.snp.remakeConstraints { make in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(aspectHeight)
            }
            // 底部工具栏视图
            footerView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalTo(bottomView.snp.top)
                make.height.equalTo(168.wmc_autoVer)
            }
        }
    }

    func setViewStyle(_ ratioLevel: YPGESunglasses.AspectRatio.RatioLevel) {
        switch ratioLevel {
        case .low: // 都不透明
            navView.backgroundColor = .white
            
            footerView.backgroundColor = .white
            footerView.isTransparent = false
            footerView.reloadItems()
            
            bottomView.backgroundColor = .white

        case .lowHeight: // 顶部透明，底部不透明
            navView.backgroundColor = .black.yp.alpha(0.3)
            
            footerView.backgroundColor = .white
            footerView.isTransparent = false
            footerView.reloadItems()
            
            bottomView.backgroundColor = .white

        case .medium: // 顶部不透明，底部透明
            navView.backgroundColor = .white
            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()
            
            bottomView.backgroundColor = .white

        case .mediumHeight: // 顶部底部全透明
            navView.backgroundColor = .black.yp.alpha(0.3)
            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()
            
            bottomView.backgroundColor = .white

        case .height: // 顶部底部全透明，模式选择也透明
            navView.backgroundColor = .black.yp.alpha(0.3)
            footerView.backgroundColor = .black.yp.alpha(0.3)
            footerView.isTransparent = true
            footerView.reloadItems()
            
            bottomView.backgroundColor = .black.yp.alpha(0.6)
        }
    }
}
